package cn.xkai.exercise.c;

/**
 * @description: 搜索旋转排序数组(已知经过旋转)
 * 自己的思路：先判断target值在旋转数组那一部分，然后在这部分进行二分查找
 * @author: kaixiang
 * @date: 2022-07-29
 **/
public class Solution115 {
    public static int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] > nums[r]) {
                if (nums[mid] > target && target >= nums[l]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {2, 1};
        int target = 0;
        System.out.println(search(nums, target));
    }
}
